<template>
  <div class="searchj-box">
    <div class="row">
      <div class="left">
        <el-icon>
          <Search />
        </el-icon>
        <span @click="router.push('/oms/index')">筛选搜索</span>
      </div>
      <div class="right">
        <el-button @click="queryReset">重置</el-button>
        <el-button type="primary" @click="searchHandle">查询搜索</el-button>
      </div>
    </div>
    <el-form>
      <el-form-item label="输入搜索：">
        <el-input v-model="form.orderSn" placeholder="订单编号" />
      </el-form-item>
      <el-form-item label="收货人：">
        <el-input
          v-model="form.receiverKeyword"
          placeholder="收货人姓名/手机号码"
        />
      </el-form-item>
      <el-form-item label="提交时间：">
        <el-date-picker
          v-model="form.createTime"
          type="date"
          label="Pick a date"
          placeholder="请选择时间"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item label="订单状态：">
        <el-select
          v-model="form.status"
          clearable
          class="m-2"
          placeholder="全部"
        >
          <el-option value="0" label="待付款"></el-option>
          <el-option value="1" label="待发货"></el-option>
          <el-option value="2" label="已发货"></el-option>
          <el-option value="3" label="已完成"></el-option>
          <el-option value="4" label="已关闭"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单分类：">
        <el-select
          v-model="form.orderType"
          clearable
          class="m-2"
          placeholder="全部"
        >
          <el-option value="0" label="正常订单"></el-option>
          <el-option value="1" label="秒杀订单"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单来源：">
        <el-select
          v-model="form.sourceType"
          clearable
          class="m-2"
          placeholder="全部"
        >
          <el-option value="0" label="PC订单"></el-option>
          <el-option value="1" label="APP订单"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
  <div class="add-area">
    <div class="text">
      <el-icon>
        <Tickets /> </el-icon
      ><span>数据列表</span>
    </div>
  </div>
  <el-table :data="list" border style="width: 100%" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55" align="center" />
    <el-table-column prop="id" label="编号" width="80" align="center" />
    <el-table-column
      prop="orderSn"
      label="订单编号"
      width="180"
      align="center"
    />
    <el-table-column
      prop="createTime"
      label="提交时间"
      width="180"
      align="center"
    >
      <template #default="{ row }">
        {{ formateDate(row.createTime) }}
      </template>
    </el-table-column>
    <el-table-column
      prop="memberUsername"
      label="用户账号"
      width="120"
      align="center"
    />
    <el-table-column
      prop="totalAmount"
      label="订单金额"
      width="120"
      align="center"
    />
    <el-table-column prop="payType" label="支付方式" width="120" align="center">
      <template #default="{ row }">
        <span v-if="row.payType === 0">未支付</span>
        <span v-else-if="row.payType === 1">支付宝</span>
        <span v-else-if="row.payType === 2">微信</span>
      </template>
    </el-table-column>
    <el-table-column
      prop="sourceType"
      label="订单来源"
      width="120"
      align="center"
    >
      <template #default="{ row }">
        <span v-if="row.sourceType === 1"> APP订单</span>
        <span v-else-if="row.sourceType === 2">PC订单</span>
      </template>
    </el-table-column>
    <el-table-column prop="status" label="订单状态" width="120" align="center">
      <template #default="{ row }">
        <span v-if="row.status === 0">待付款</span>
        <span v-else-if="row.status === 1">待发货</span>
        <span v-else-if="row.status === 2">已发货</span>
        <span v-else-if="row.status === 3">已完成</span>
        <span v-else>已关闭</span>
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="200" align="center">
      <template #default="{ row }">
        <el-button size="small" @click="router.push('/oms/orderDetail?id=' + row.id)">查看订单</el-button>
        <el-button size="small" type="danger" v-if="row.status === 4" @click="del(row.id)">删除订单</el-button> 
         <el-button size="small" v-if="row.status !== 4" @click="showTrack = true">订单跟踪</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="foot">
    <div class="left">
      <el-select class="m-2" v-model="optionList" clearable  placeholder="批量操作">
        <el-option v-for="item in options" ::key="item.value" :value="item.value" :label="item.label" />
      </el-select>
      <el-button type="primary" @click="ConfirmTn">确定</el-button>
    </div>
    <div class="page">
      <el-pagination
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="pageChangeHandle"
        @current-change="pageSizeChangeHandle"
      />
    </div>
  </div>
  <orderRemarks :isShow="showDialog" @hide="closeDialog" :deleteList="deleteList" />
  <orderTrackings :isShow="showTrack" @hide="closeTrack" />
</template>
<script setup lang='ts'>
import useTable from '@/hooks/useTable'
import { ElMessageBox, ElMessage } from 'element-plus';
import { ref, reactive } from 'vue'
import { Search, Tickets } from "@element-plus/icons-vue"
import { ElConfigProvider } from 'element-plus'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import { formateDate } from '@/utils'
import { getOrderListApi, DeleteOrderApi,getOrderDeleteTwoApi } from './api'
import { useRouter } from "vue-router"
import orderRemarks from "./orderRemarks.vue"
import orderTrackings from "./orderTracking.vue"
const router = useRouter()
// 获取搜索内容
const form = reactive({
  orderSn: "",
  receiverKeyword: "",
  createTime: "",
  status: '',
  orderType: '',
  sourceType: ''
})


const showDialog = ref(false);
const closeDialog = (isReload?: boolean) => {
  showDialog.value = false;
  if (isReload) getOrderList();
};
const showTrack = ref(false);
const closeTrack = (isReload?: boolean) => {
  showTrack.value = false;
  if (isReload) getOrderList();
};
const orderTrack = () => {
  showTrack.value = true;
};

const status = ref([])
const orderType = ref([])
const sourceType = ref([])
const deleteList = ref<number[]>([])
const optionList = ref([])
const options = [
    { value: '0', label: '批量发货', },
    { value: '1', label: '关闭订单', },
    { value: '2', label: '删除订单', },
]
const del = (id: number) => {
  ElMessageBox.confirm('是否要进行该删除操作?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    DeleteOrderApi(id).then((res) => {
      if (res.code === 200) {
        ElMessage.success('删除成功');
        getOrderList();
      }
    })
  }).catch(() => { })

}
// 获取复选框状态id
const handleSelectionChange = (val: OrderModel[]) => {
    val.forEach(item => {
        list.value.find(sur => sur.id === item.id)
        deleteList.value.push(item.id)
    })
}
// 重置
const queryReset = () => {
    form.orderSn = '';
    form.receiverKeyword = '';
    form.createTime = '';
    form.status= '';
    form.orderType= '';
    form.sourceType= '';
}
// 批量操作
const ConfirmTn = () => {
    if (deleteList.value.length == 0) {
        ElMessage.warning('请选择要操作的订单')
        return
    }
    if (optionList.value[0] == 2) {
        getOrderDeleteTwoApi(deleteList.value).then(res => {
            if (res.code === 200) {
                ElMessage.success('删除成功')
                getOrderList();
            }
        })
    } else if (optionList.value[0] == 1) {
        showDialog.value = true
    }
}
// 封装请求数据
const getOrderList = () => {
    getOrderListApi(
        {
            pageNum: 1,
            pageSize: 10,
            status: status.value[0],
            orderType: orderType.value[0],
            sourceType: sourceType.value[0],
            orderSn: form.orderSn,
            receiverKeyword: form.receiverKeyword,
            createTime: form.createTime
        }
    ).then(res => {
        if (res.code === 200) {
            list.value = res.data.list
            total.value = res.data.total
        }
    })
}
const { list, pageNum, total, pageSize, pageChangeHandle, pageSizeChangeHandle, searchHandle } = useTable(getOrderListApi, form);

</script>
<style lang="less" scoped>
.searchj-box {
  box-shadow: 0 0 4px #ccc;
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;

  .row {
    padding: 20px 0;
    display: flex;

    &:first-child {
      justify-content: space-between;
    }

    &:last-child {
      width: 400px;

      span {
        width: 100px;
      }
    }
  }

  :deep(.el-form) {
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    width: 1073px;

    :deep(.el-form-item) {
      width: 32%;
    }
  }
}

.add-area {
  display: flex;
  justify-content: space-between;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 0 4px #ccc;
  border-radius: 10px;

  .text {
    display: flex;
    align-items: center;

    span {
      padding-left: 10px;
    }
  }
}

.foot {
  display: flex;
  justify-content: space-between;
  padding-top: 30px;
}
</style>
